package com.yyk.yykywd.ui.work;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;
import com.yyk.yykywd.R;
import com.yyk.yykywd.util.OnClickFlagUtil;
import com.yyk.yykywd.util.SingleOptionsPickerUtil;
import com.yyk.yykywd.base.BaseActivity;
import com.yyk.yykywd.config.Constant;
import com.yyk.yykywd.http.ApiService;
import com.yyk.yykywd.bean.WorkBusinessfollowInfo;
import com.yyk.yykywd.util.Md5Util2;
import com.yyk.yykywd.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 业务跟进
 */
public class BusinessToGoActivity extends BaseActivity {


    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.ll_show)
    LinearLayout llShow;
    @BindView(R.id.et_describe)
    EditText etDescribe;
    @BindView(R.id.et_remark)
    EditText etRemark;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_postname)
    EditText etPostname;
    @BindView(R.id.et_phone)
    EditText etPhone;
    private int typeValue = 1;
    private String id;

    @Override
    public int getLayoutId() {
        return R.layout.activity_business_to_go;
    }

    @Override
    public void initView() {
        initToolbar();

    }

    private void initToolbar() {
        setBackArrow();
        setTitle("业务跟进");
    }

    @Override
    public void initData() {
        initGetIntent();

    }

    private void initGetIntent() {
        if (getBundle() != null) {
            id = getBundle().getString("id");
        }
    }


    @OnClick({R.id.ll_type, R.id.bt_save})
    public void onViewClicked(View view) {
        if (OnClickFlagUtil.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.ll_type:
                List<String> list = new ArrayList<>();
                list.add("再次拜访");
                list.add("电话回访");
                list.add("其他跟进");
                SingleOptionsPickerUtil.openOptionsPicker(this, list, 1, tvType);
                SingleOptionsPickerUtil.setOnPicker(new SingleOptionsPickerUtil.Listenter() {
                    @Override
                    public void lisenter(String value) {
                        if (value.equals("电话回访")) {
                            llShow.setVisibility(View.GONE);
                            typeValue = 2;
                        } else if (value.equals("其他跟进")) {
                            llShow.setVisibility(View.GONE);
                            typeValue = 3;
                        } else {
                            llShow.setVisibility(View.VISIBLE);
                            typeValue = 1;
                        }
                    }
                });


                break;
            case R.id.bt_save:
                /**
                 *  13. 业务跟进
                 */
                if (TextUtils.isEmpty(etDescribe.getText())) {
                    ToastUtil.showShort("请填写跟进内容");
                    return;
                }
                if (TextUtils.isEmpty(etRemark.getText())) {
                    ToastUtil.showShort("请填写跟进结果");
                    return;
                }
                Map<String, String> map = new TreeMap<>();
                map.put("uid", Hawk.get("uid")+"");
                map.put("bid", id + "");
                map.put("describe", etDescribe.getText().toString().trim() + "");
                map.put("remark", etRemark.getText().toString().trim());
                map.put("type", typeValue + "");
                if (typeValue == 1) {
                    map.put("name", etName.getText().toString().trim());
                    map.put("post_name", etPostname.getText().toString().trim());
                    map.put("phone", etPhone.getText().toString().trim());
                }
                map.put("sign", Md5Util2.createSign(map));
                ApiService.getInstance().api.postWorkBusinessfollow(map)
                        .subscribeOn(Schedulers.io())     //调度线程
                        .observeOn(AndroidSchedulers.mainThread())
                        .unsubscribeOn(Schedulers.io())   //取消订阅
                        .subscribe(new Observer<WorkBusinessfollowInfo>() {
                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(WorkBusinessfollowInfo info) {
                                Logger.e("业务跟进" + info.toString());
                                ToastUtil.showShort(info.getMsg());
                                if (info.getCode() == 1) {
                                    finish();
                                }
                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onComplete() {

                            }
                        });

                break;
        }
    }


}
